package cn.danger.com.model.action;

import android.content.Context;
import android.text.TextUtils;

import cn.bmob.v3.BmobSMS;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.QueryListener;
import cn.bmob.v3.listener.UpdateListener;
import cn.danger.com.model.entity.MyUser;
import cn.danger.com.model.listener.OnCommonListener;
import cn.danger.com.util.Check;
import cn.danger.com.util.Constants;
import cn.danger.com.util.MyNetManager;

/**
 * Created by 俊 on 2016/12/30.
 */
public class ModifyPhoneModelImpl implements ModifyPhoneModel {

    private Context context;

    public ModifyPhoneModelImpl(Context context) {
        this.context = context;
    }

    private String check(String phone, String check_code) {
        if (TextUtils.isEmpty(phone)) {
            return Constants.PHONE_IS_EMPTY;
        }
        if (!Check.isMobileNO(phone)) {
            return Constants.PHONE_IS_ILLEGAL;
        }
        if (TextUtils.isEmpty(check_code)) {
            return Constants.CHECK_CODE_IS_EMPTY;
        }
        return Constants.IS_RIGHT;
    }

    @Override
    public void sendCheckCode(String phone, final OnCommonListener listener) {
        if (TextUtils.isEmpty(phone)) {
            listener.onError(Constants.PHONE_IS_EMPTY);
        }
        if (!Check.isMobileNO(phone)) {
            listener.onError(Constants.PHONE_IS_ILLEGAL);
        }
        if (!new MyNetManager(context).isNetworkAvailable()) {
            listener.onError(Constants.NETWORK_IS_NOT_AVAILABLE);
        } else {
            MyUser user = BmobUser.getCurrentUser(MyUser.class);
            if (phone.equals(user.getMobilePhoneNumber())) {
                listener.onError(Constants.PHONE_IS_USING);
            } else {
                BmobSMS.requestSMSCode(phone, "Schedule", new QueryListener<Integer>() {
                    @Override
                    public void done(Integer integer, BmobException e) {
                        if (e == null) {
                            listener.onSuccess(null);
                        } else {
                            e.printStackTrace();
                            if (Check.checkByCode(e.getErrorCode()) == null) {
                                listener.onError(Constants.FAIL_TO_SEND_CHECK);
                            } else {
                                listener.onError(Check.checkByCode(e.getErrorCode()));
                            }
                        }
                    }
                });
            }
        }
    }

    @Override
    public void updatePhone(final String phone, String check_code, final OnCommonListener listener) {
        String check_result = check(phone, check_code);
        if (check_result.equals(Constants.IS_RIGHT)) {
            if (new MyNetManager(context).isNetworkAvailable()) {
                MyUser user = BmobUser.getCurrentUser(MyUser.class);
                if (phone.equals(user.getMobilePhoneNumber())) {
                    listener.onError(Constants.PHONE_IS_USING);
                } else {
                    MyUser new_user = new MyUser();
                    new_user.setUsername(phone);
                    new_user.setMobilePhoneNumber(phone);
                    new_user.setMobilePhoneNumberVerified(true);
                    if (user.getNick_name().equals(user.getMobilePhoneNumber())) {
                        new_user.setNick_name(phone);
                    }
                    new_user.update(user.getObjectId(), new UpdateListener() {
                        @Override
                        public void done(BmobException e) {
                            if (e == null) {
                                listener.onSuccess(null);
                            } else {
                                e.printStackTrace();
                                if (Check.checkByCode(e.getErrorCode()) == null) {
                                    listener.onError(Constants.FAIL_TO_BIND_PHONE);
                                } else {
                                    listener.onError(Check.checkByCode(e.getErrorCode()));
                                }
                            }
                        }
                    });
                }
            } else {
                listener.onError(Constants.NETWORK_IS_NOT_AVAILABLE);
            }
        } else {
            listener.onError(check_result);
        }
    }
}
